package com.example.openharmony.controller;

import com.example.openharmony.entity.Product;
import com.example.openharmony.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 商品控制器
 * 处理商品相关的接口
 */
@RestController
@RequestMapping("/api/products")
public class ProductController {
    
    @Autowired
    private ProductService productService;
    
    /**
     * 获取商品列表
     * @param status 商品状态筛选
     * @param category 商品分类筛选
     * @return 商品列表
     */
    @GetMapping
    public Map<String, Object> getProducts(
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String category) {
        
        List<Product> products;
        if (status != null) {
            products = productService.findByStatus(status);
        } else if (category != null) {
            products = productService.findByCategory(category);
        } else {
            products = productService.findAll();
        }
        
        return Map.of("success", true, "data", products);
    }
    
    /**
     * 根据ID获取商品详情
     * @param id 商品ID
     * @return 商品详情
     */
    @GetMapping("/{id}")
    public Map<String, Object> getProductById(@PathVariable Long id) {
        Optional<Product> productOpt = productService.findById(id);
        if (productOpt.isPresent()) {
            return Map.of("success", true, "data", productOpt.get());
        } else {
            return Map.of("success", false, "message", "商品不存在");
        }
    }
    /**
     * 发布商品
     * @param product 商品信息
     * @return 发布结果
     */
    @PostMapping
    public Map<String, Object> createProduct(@RequestBody Product product) {
        // TODO: 实际项目中需要从认证信息中获取卖家ID
        product.setSellerId(1L); // 临时设置卖家ID
        product.setStatus("待售"); // 默认状态为待售
        
        Product savedProduct = productService.save(product);
        return Map.of("success", true, "message", "商品发布成功", "data", savedProduct);
    }
    /**
     * 更新商品
     * @param id 商品ID
     * @param product 商品信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public Map<String, Object> updateProduct(@PathVariable Long id, @RequestBody Product product) {
        Optional<Product> existingProductOpt = productService.findById(id);
        if (existingProductOpt.isPresent()) {
            Product existingProduct = existingProductOpt.get();
            // 更新商品信息
            existingProduct.setTitle(product.getTitle());
            existingProduct.setDescription(product.getDescription());
            existingProduct.setPrice(product.getPrice());
            existingProduct.setCategory(product.getCategory());
            existingProduct.setStatus(product.getStatus());
            Product updatedProduct = productService.save(existingProduct);
            return Map.of("success", true, "message", "商品更新成功", "data", updatedProduct);
        } else {
            return Map.of("success", false, "message", "商品不存在");
        }
    }
}